package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.common.MyThreadLocal;
import com.itheima.common.R;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    //定义一个方法//接收请求参数
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {
        //获取用户id:1.session 2.threadlocal
        Long userId = MyThreadLocal.getCurrentId();
        //获取dishId/setmealId
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        //判断是菜品还是套餐
        if (dishId != null) {
            //是菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //是套餐
            //是菜品
            queryWrapper.eq(ShoppingCart::getSetmealId, setmealId);
        }
        //根据dishId和UserId查询数据表的数据
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        //判断是否是第一次添加购物车
        if (one != null) {
            //不是第一次 就将购物车的number+1
            one.setNumber(one.getNumber() + 1);
            //shoppingCart.setUserId(userId);
            shoppingCartService.updateById(one);
        } else {
            //第一次就将保存到购物车
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
        }
        return R.success(shoppingCart);
    }


    //判断是否是第一次添加购物车

    //不是第一次 就将购物车的number+1
    //将结果响应给前端

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
   /* @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {
        //获取当前登录的用户
        Long userId = MyThreadLocal.getCurrentId();
        //给购物车对象赋值
        shoppingCart.setUserId(userId);

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //根据登录的userId查询
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        if (shoppingCart.getDishId() != null) {
            //查到的是菜品dish
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            //查到的是套餐setmeal
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart one = shoppingCartService.getOne(queryWrapper);

        if (one != null) {
            //查到了--给数量加1
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            //修改保存
            shoppingCartService.updateById(one);
        } else {
            //第一次点餐--设置number为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            //再将接收到的shoppingCart的信息赋值给one
            one = shoppingCart;
        }
        //将查询到的one响应给前端
        return R.success(one);
    }*/

    /**
     * 查看购物车
     *
     * @return
     */
    @GetMapping("/list")
    public R list() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //select * from shoppingCart where userId = ?
        queryWrapper.eq(ShoppingCart::getUserId, MyThreadLocal.getCurrentId());
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(queryWrapper);
        return R.success(shoppingCarts);
    }

    /**
     * 删除购物车数据
     */
    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart) {
        //获取当前登录的用户Id
        Long userId = MyThreadLocal.getCurrentId();
        //判断要删除的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //update from shoppingCart set number = number-1 where userId = ? and dishId = ?
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        if (dishId != null) {
            //是菜品--删除菜品--菜品的数量-1
            //select * from shoppingCart where  userId = ? and dishId = ?
            //根据userId和dishId查询shoppingCart对象
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //是套餐
            //select * from shoppingCart where  userId = ? and setmealId = ?
            //根据userId和dishId查询shoppingCart对象
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        if (cart.getNumber() == 1) {
            shoppingCartService.removeById(cart);
        } else {
            //获取购物车里该菜品的数量
            Integer number = cart.getNumber() - 1;
            cart.setNumber(number);
            shoppingCartService.updateById(cart);
        }
        return R.success(cart);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R clean() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, MyThreadLocal.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空成功");
    }

}
